class TextUtil {
  constructor(dataList) {
    this.dataList = dataList
  }

  getWords(pageIndex) {
    let data = this.dataList[pageIndex]
    if (!data) return
    let text = data.$text
    if (!text) return
    if (!text.words) return
    return text.words.map((item, index) => {
      let x1 = item.x1
      let x2 = item.x2
      let y1 = item.y1
      let y2 = item.y2
      return {
        ...item,
        x1: Math.min(x1, x2),
        x2: Math.max(x1, x2),
        y1: Math.min(y1, y2),
        y2: Math.max(y1, y2),
        index,
        pageIndex
      }
    })
  }

  getWordByImgPos(pageIndex, imgPos) {
    let words = this.getWords(pageIndex)
    if (!words) return

    for (let i = 0; i < words.length; i++) {
      let word = words[i]
      //字内
      if (imgPos.x >= word.x1 && imgPos.x <= word.x2 && imgPos.y >= word.y1 && imgPos.y <= word.y2) {
        return {
          position: imgPos.x > (word.x1 + word.x2) / 2 ? 'right' : 'left',
          ...word
        }
      }
    }
    let prevRowIndex = -1
    let minLineHeight = Number.MAX_VALUE
    for (let i = 0; i < words.length; i++) {
      let word = words[i]
      // 行内
      if (imgPos.y >= word.y1 && imgPos.y <= word.y2) {
        prevRowIndex = word.rowIndex
        break
      } else {
        // 判断最小间隙
        if (prevRowIndex !== word.rowIndex) {
          // 换行了
          if (imgPos.y - word.y2 > 0) {
            // 鼠标上面
            if (imgPos.y - word.y2 < minLineHeight) {
              minLineHeight = imgPos.y - word.y2
              prevRowIndex = word.rowIndex
            }
          } else if (word.y1 - imgPos.y > 0) {
            // 鼠标下面
            if (word.y1 - imgPos.y < minLineHeight) {
              minLineHeight = word.y1 - imgPos.y
              prevRowIndex = word.rowIndex
            }
          }
        }
      }
    }
    // 文字行prevRowIndex
    let rowWords = words.filter(word => prevRowIndex === word.rowIndex)

    let prevWordIndex = -1
    let minLineWidth = Number.MAX_VALUE

    for (let i = 0; i < rowWords.length; i++) {
      // 字内 // 字的左右之间
      let word = rowWords[i]
      if (imgPos.x >= word.x1 && imgPos.x <= word.x2) {
        return {
          position: imgPos.x > (word.x1 + word.x2) / 2 ? 'right' : 'left',
          ...word
        }
      } else {
        let leftSpace = Math.abs(imgPos.x - word.x1)
        let rightSpace = Math.abs(imgPos.x - word.x2)
        let min = Math.min(leftSpace, rightSpace)
        if (min < minLineWidth) {
          minLineWidth = min
          prevWordIndex = i
        }
      }
    }

    let resWord = rowWords[prevWordIndex]

    if (Math.abs(imgPos.x - resWord.x1) < Math.abs(imgPos.x - resWord.x2)) {
      return {
        position: 'left',
        ...resWord
      }
    } else {
      return {
        position: 'right',
        ...resWord
      }
    }
  }

  getWordByIndexPos(pos) {
    if (/^(\d+)-(\d+)$/.test(pos)) {
      let pageIndex = Number(RegExp.$1)
      let index = Number(RegExp.$2)

      let words = this.getWords(pageIndex)
      if (!words) return
      let word = words[index]
      if (!word) return
      return {
        ...word,
        pageIndex,
        index
      }
    }
  }

  getWrodsAToB(A, B) {
    if (!A || !B) return
    if (A.pageIndex > B.pageIndex) {
      [A, B] = [B, A]
    } else if (A.pageIndex === B.pageIndex) {
      if (A.index > B.index) {
        [A, B] = [B, A]
      }
    }
    let tRows = {}

    // AB之间的页
    for (let pageIndex = A.pageIndex; pageIndex <= B.pageIndex; pageIndex++) {
      let words = this.getWords(pageIndex)
      if (!words) return
      let start = 0
      let end = words.length - 1
      if (pageIndex === A.pageIndex && pageIndex === B.pageIndex) {
        start = A.index
        end = B.index
      } else if (pageIndex === A.pageIndex) {
        start = A.index
      } else if (pageIndex === B.pageIndex) {
        end = B.index
      }
      for (let index = start; index <= end; index++) {
        let word = words[index]

        if (B.pageIndex === A.pageIndex && B.index === A.index) {
          if (A.position && B.position && A.position === B.position) break
        } else {
          if (pageIndex === A.pageIndex && index === A.index) {
            if (A.position === 'right') continue
          }
          if (pageIndex === B.pageIndex && index === B.index) {
            if (B.position === 'left') continue
          }
        }

        let name = `${pageIndex}_${word.rowIndex}`

        // console.log(word)
        if (!tRows[name]) {
          tRows[name] = {
            page: pageIndex,
            x: word.x1,
            y: word.y1,
            width: 0,
            height: 0,
            text: '',
            start: `${pageIndex}-${index}`,
            end: ''
          }
        }
        tRows[name].width = word.x2 - tRows[name].x
        tRows[name].height = word.y2 - tRows[name].y
        tRows[name].text += word.text
        tRows[name].end = `${pageIndex}-${index}`
      }
    }
    return tRows
  }

  getPos(A, B) {
    if (A.pageIndex > B.pageIndex) {
      [A, B] = [B, A]
    } else if (A.pageIndex === B.pageIndex) {
      if (A.index > B.index) {
        [A, B] = [B, A]
      } else if (A.index === B.index) {
        return null
      }
    }
    if (A.pageIndex === B.pageIndex && A.index === B.index - 1) {
      if (A.position === 'right' && B.position === 'left') {
        return
      }
    }
    let start = `${A.pageIndex}-${A.index}`
    let end = `${B.pageIndex}-${B.index}`
    if (A.position === 'right') {
      let words = this.getWords(A.pageIndex)
      if (!words) return null
      if (A.index === words.length - 1) {
        // 最后一个字右边
        return null
      }
      start = `${A.pageIndex}-${A.index + 1}`
    }
    if (B.position === 'left') {
      if (B.index === 0) {
        // 第一个字左边
        return null
      }
      end = `${B.pageIndex}-${B.index - 1}`
    }

    return {start, end}
  }
}

export default TextUtil
